home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP05.ZIP / CHAP05 / PATRON / PAGE.CPP < prev    next >
C/C++ Source or Header  |  1993-06-07  |  4KB  |  221 lines

  1. /*
  2.  * PAGE.CPP
  3.  * Modifications for Chapter 5
  4.  *
  5.  * Implementation of the CPage class which is a simple structure.
  6.  *
  7.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  8.  *
  9.  * Kraig Brockschmidt, Software Design Engineer
  10.  * Microsoft Systems Developer Relations
  11.  *
  12.  * Internet  :  kraigb@microsoft.com
  13.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  14.  */
  15.  
  16.  
  17. #include "patron.h"
  18.  
  19.  
  20.  
  21. /*
  22.  * CPage::CPage
  23.  * CPage::~CPage
  24.  *
  25.  * Constructor Parameters:
  26.  *  dwID            DWORD identifier for this page.
  27.  */
  28.  
  29. CPage::CPage(DWORD dwID)
  30.     {
  31.     //CHAPTER5MOD
  32.     m_dwID     =dwID;
  33.     m_pIStorage=NULL;
  34.     //End CHAPTER5MOD
  35.     return;
  36.     }
  37.  
  38. CPage::~CPage(void)
  39.     {
  40.     //CHAPTER5MOD
  41.     Close(FALSE);
  42.     //End CHAPTER5MOD
  43.     return;
  44.     }
  45.  
  46.  
  47.  
  48. /*
  49.  * CPage::GetID
  50.  *
  51.  * Return Value:
  52.  *  DWORD           dwID field in this page.  This function is only here
  53.  *                  to avoid hiding inline implementations in pages.h
  54.  */
  55.  
  56. DWORD CPage::GetID(void)
  57.     {
  58.     return m_dwID;
  59.     }
  60.  
  61.  
  62.  
  63.  
  64. //CHAPTER5MOD
  65.  
  66. /*
  67.  * CPage::FOpen
  68.  *
  69.  * Purpose:
  70.  *  Retrieves the IStorage associated with this page.  The IStorage is
  71.  *  owned by the page and thus the page always holds a reference count.
  72.  *  The caller should call ::Close or delete this page to match this open.
  73.  *
  74.  *  This function may be called multiple times resulting in additional
  75.  *  reference counts on the storage each of which must be matched with
  76.  *  a call to ::Close.  The last ::Close can be done through delete.
  77.  *
  78.  * Parameters:
  79.  *  pIStorage       LPSTORAGE in which this page lives.
  80.  *
  81.  * Return Value:
  82.  *  BOOL            TRUE if opening succeeds, FALSE otherwise.
  83.  */
  84.  
  85. BOOL CPage::FOpen(LPSTORAGE pIStorage)
  86.     {
  87.     BOOL        fNULL=FALSE;
  88.     HRESULT     hr=NOERROR;
  89.     DWORD       dwMode=STGM_TRANSACTED | STGM_READWRITE | STGM_SHARE_EXCLUSIVE;
  90.     char        szTemp[32];
  91.  
  92.     if (NULL==m_pIStorage)
  93.         {
  94.         fNULL=TRUE;
  95.  
  96.         if (NULL==pIStorage)
  97.             return FALSE;
  98.  
  99.         /*
  100.          * Attempt to open the storage under this ID.  If there is none, then
  101.          * create it.  In either case we end up with an IStorage that we
  102.          * either save in pPage or release.
  103.          */
  104.  
  105.         wsprintf(szTemp, "Page %lu", m_dwID);
  106.  
  107.         hr=pIStorage->OpenStorage(szTemp, NULL, dwMode, NULL, 0, &m_pIStorage);
  108.  
  109.         if (FAILED(hr))
  110.             hr=pIStorage->CreateStorage(szTemp, dwMode, 0, 0, &m_pIStorage);
  111.         }
  112.     else
  113.         m_pIStorage->AddRef();
  114.  
  115.     if (FAILED(hr))
  116.         {
  117.         if (fNULL)
  118.             m_pIStorage=NULL;
  119.  
  120.         return FALSE;
  121.         }
  122.  
  123.     return TRUE;
  124.     }
  125.  
  126.  
  127.  
  128.  
  129. /*
  130.  * CPage::Close
  131.  *
  132.  * Purpose:
  133.  *  Possibly commits the storage, then releases it reversing the
  134.  *  reference count from FOpen.
  135.  *
  136.  * Parameters:
  137.  *  fCommit         BOOL indicating if we're to commit.
  138.  *
  139.  * Return Value:
  140.  *  None
  141.  */
  142.  
  143. void CPage::Close(BOOL fCommit)
  144.     {
  145.     if (NULL==m_pIStorage)
  146.         return;
  147.  
  148.     if (fCommit)
  149.         Update();
  150.  
  151.     if (0==m_pIStorage->Release())
  152.         m_pIStorage=NULL;
  153.  
  154.     return;
  155.     }
  156.  
  157.  
  158.  
  159.  
  160. /*
  161.  * CPage::Update
  162.  *
  163.  * Purpose:
  164.  *  Forces a common on the page if it's open.
  165.  *
  166.  * Parameters:
  167.  *  None
  168.  *
  169.  * Return Value:
  170.  *  BOOL            Always TRUE for now.
  171.  */
  172.  
  173. BOOL CPage::Update(void)
  174.     {
  175.     if (NULL!=m_pIStorage)
  176.         m_pIStorage->Commit(STGC_ONLYIFCURRENT);
  177.  
  178.     return TRUE;
  179.     }
  180.  
  181.  
  182.  
  183.  
  184.  
  185. /*
  186.  * CPage::Destroy
  187.  *
  188.  * Purpose:
  189.  *  Removes this page from the given storage.  The caller should
  190.  *  eventually delete this Page object to free the storage.
  191.  *
  192.  * Parameters:
  193.  *  pIStorage       LPSTORAGE contianing this page on which to call
  194.  *                  ::DestroyElement
  195.  *
  196.  * Return Value:
  197.  *  None
  198.  */
  199.  
  200. void CPage::Destroy(LPSTORAGE pIStorage)
  201.     {
  202.     char        szTemp[32];
  203.  
  204.     if (NULL!=pIStorage)
  205.         {
  206.         if (NULL!=m_pIStorage)
  207.             m_pIStorage->Release();
  208.  
  209.         wsprintf(szTemp, "Page %lu", m_dwID);
  210.         pIStorage->DestroyElement(szTemp);
  211.  
  212.         m_pIStorage=NULL;
  213.         }
  214.  
  215.     return;
  216.     }
  217.  
  218.  
  219.  
  220. //End CHAPTER5MOD
  221.